package com.xhwl.logistics.service.integrate;


import com.huaweicloud.sdk.core.auth.BasicCredentials;
import com.huaweicloud.sdk.core.auth.ICredential;
import com.huaweicloud.sdk.core.exception.ConnectionException;
import com.huaweicloud.sdk.core.exception.RequestTimeoutException;
import com.huaweicloud.sdk.core.exception.ServiceResponseException;
import com.huaweicloud.sdk.iotda.v5.IoTDAClient;
import com.huaweicloud.sdk.iotda.v5.model.CreateCommandRequest;
import com.huaweicloud.sdk.iotda.v5.model.CreateCommandResponse;
import com.huaweicloud.sdk.iotda.v5.model.DeviceCommandRequest;
import com.huaweicloud.sdk.iotda.v5.region.IoTDARegion;
import com.xhwl.common.dto.cent.pedes.AuthDTO;
import com.xhwl.common.dto.cent.pedes.UserDTO;
import com.xhwl.common.utils.StringUtils;
import org.springframework.beans.factory.annotation.Value;

import java.util.HashMap;
import java.util.Map;


public class IoTJieShunService {

    @Value(value = "${iot-jieshun.region}")
    private String region;
    @Value(value = "${iot-jieshun.ak}")
    private String ak;
    @Value(value = "${iot-jieshun.sk}")
    private String sk;
    @Value(value = "${iot-jieshun.userDeviceId}")
    private String userDeviceId;
    @Value(value = "${iot-jieshun.pedestrianAuthDeviceId}")
    private String pedestrianAuthDeviceId;

    private final String SERVICE_USER = "UserManger";
    private final String SERVICE_AUTHORIZATION = "AuthorizationManger";
    private final String COMMAND_SAVE = "CMD_Save";
    private final String COMMAND_DELETE = "CMD_Delete";

    /**
     * 保存人员信息
     *
     * @param userDTO
     * @return
     */
    public String userSave(UserDTO userDTO) {
        Map<String, Object> parasMap = new HashMap<>();
        if (!StringUtils.isEmpty(userDTO.getUserId())) {
            parasMap.put("UserId", userDTO.getUserId());
        }
        parasMap.put("Name", userDTO.getName());
        parasMap.put("Phone", userDTO.getPhone());
        parasMap.put("FaceUrl", userDTO.getFaceUrl());
        String result = this.command(userDeviceId, SERVICE_USER, COMMAND_SAVE, parasMap);
        return result;
    }

    /**
     * 删除人员信息
     *
     * @param userOpenId
     * @return
     */
    public String userDelete(String userOpenId) {
        Map<String, Object> parasMap = new HashMap<>();
        parasMap.put("UserId", userOpenId);
        String result = this.command(userDeviceId, SERVICE_USER, COMMAND_DELETE, parasMap);
        return result;
    }

    /**
     * 保存授权信息
     *
     * @param authDTO
     * @return
     */
    public String pedestrianAuthSave(AuthDTO authDTO) {
        Map<String, Object> parasMap = new HashMap<>();
        parasMap.put("UserId", authDTO.getUserOpenId());
        parasMap.put("CardNum", authDTO.getCardNumber());
        parasMap.put("DoorIdList", authDTO.getDeviceIdList());
        String result = this.command(pedestrianAuthDeviceId, SERVICE_AUTHORIZATION, COMMAND_SAVE, parasMap);
        return result;
    }

    /**
     * 删除授权信息
     *
     * @param authDTO
     * @return
     */
    public String pedestrianAuthDelete(AuthDTO authDTO) {
        Map<String, Object> parasMap = new HashMap<>();
        parasMap.put("UserId", authDTO.getUserOpenId());
        String result = this.command(pedestrianAuthDeviceId, SERVICE_AUTHORIZATION, COMMAND_DELETE, parasMap);
        return result;
    }

    private String command(String deviceId, String serviceId, String commandName, Object paras) {
        ICredential auth = new BasicCredentials()
                .withAk(ak)
                .withSk(sk);

        IoTDAClient client = IoTDAClient.newBuilder()
                .withCredential(auth)
                .withRegion(IoTDARegion.valueOf(region))
                .build();
        CreateCommandRequest request = new CreateCommandRequest();
        request.withDeviceId(deviceId);
        DeviceCommandRequest body = new DeviceCommandRequest();
        body.withCommandName(commandName);
        body.withServiceId(serviceId);
        body.withParas(paras);
        request.withBody(body);
        CreateCommandResponse response = null;
        try {
            response = client.createCommand(request);
            //TODO 返回数据判断

        } catch (ConnectionException e) {
            e.printStackTrace();
        } catch (RequestTimeoutException e) {
            e.printStackTrace();
        } catch (ServiceResponseException e) {
            e.printStackTrace();
        }
        return response.toString();
    }
}